package com.activitycode.fragment;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.activitycode.datamodel.RegionInfo;
import com.activitycode.datamodel.model.DetailPetImages;
import com.activitycode.datamodel.model.ListInfo;
import com.activitycode.datamodel.model.UserInfo;
import com.activitycode.util.CommConstant;
import com.activitycode.util.DataBaseHelper;
import com.activitycode.util.SharedPreferrenceManager;
import com.duostec.zhuyue.pets.R;
import com.j256.ormlite.dao.Dao;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class GridAdapter extends BaseAdapter{
	private List<String> list =new ArrayList<String>();
	private List<String> userlist =new ArrayList<String>();
	private Context mContext;
	private ImageLoader imageLoader;
	private DisplayImageOptions options,options2;
	private int width;
	
	
	
	
	
	public GridAdapter(Context mContext, List<String> list,List<String> userlist,ImageLoader imageLoader,int width) {
		this.mContext = mContext;
		this.list = list;
		this.userlist=userlist; 
		this.imageLoader = imageLoader;
		this.width = width;
		this.options = new DisplayImageOptions.Builder()		
		.cacheInMemory(true)
		.cacheOnDisc(true)
		.considerExifParams(true)
		.showImageOnLoading(mContext.getResources().getDrawable(R.drawable.head_pet))
//		.displayer(new RoundedBitmapDisplayer(100))
		.build();
	}
	

	public int getCount() {
		return this.list.size();
	}

	public Object getItem(int position) {
		return list.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(final int position, View view, ViewGroup arg2) {
		try {
			ViewHolder viewHolder = null;
			if (view == null) {
				viewHolder = new ViewHolder();
				view = LayoutInflater.from(mContext).inflate(R.layout.adapter_imageview2, null);			
				viewHolder.head = (ImageView) view.findViewById(R.id.adapter_imageView_img);
				viewHolder.smallhead = (ImageView) view.findViewById(R.id.adapter_imageView_smallimg);
				view.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) view.getTag();
			}
			int w = (width-20)/3;
			viewHolder.head.getLayoutParams().height = w;
			if(list.get(position)!=null){
				if(list.get(position).equals("add")){
					viewHolder.head.setImageDrawable(mContext.getResources().getDrawable(R.drawable.pet_camera));
				}else{
					this.imageLoader.displayImage(CommConstant.SERVICE_HOST_IP_LAN_IMG+list.get(position), viewHolder.head, options);
					initOptions();
		        	this.imageLoader.displayImage(CommConstant.SERVICE_HOST_IP_LAN_IMG+userlist.get(position), viewHolder.smallhead, options2);

				}
				
	        }
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return view;
	}
	private void initOptions(){
		options2 = new DisplayImageOptions.Builder()		
		.cacheInMemory(true)
		.cacheOnDisc(true)
		.considerExifParams(true)
		.displayer(new RoundedBitmapDisplayer(100))
		.build();
	}


	final static class ViewHolder {

		ImageView head,smallhead;
	}


	
}